package gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddContactDialog extends JDialog implements ActionListener
{
	public static enum RETURN_MODES {MB_OK, MB_CANCEL};
	
	protected JLabel lblContactName = new JLabel("Contact Name: ");
	protected JTextField txtContactName = new JTextField();
	protected JButton btnOK = new JButton("OK");
	protected JButton btnCancel = new JButton("Cancel");
	
	public String returnValue = "";
	public RETURN_MODES returnMode = RETURN_MODES.MB_CANCEL;
	
	public AddContactDialog(Frame owner)
	{
		super(owner, true); // modal
		
		// set up the UI
		Container content = getContentPane();
		GridBagConstraints c = new GridBagConstraints();		
		GridBagLayout layout = new GridBagLayout();
		
		setLayout(layout);
		setTitle("Add A Contact");
		
		// "add contact" label
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.NONE;
		content.add(lblContactName, c);
		
		// the contact name text field
		c.gridx = 1;
		c.gridy = 0;
		c.fill = GridBagConstraints.NONE;
		content.add(txtContactName, c);
		
		// The OK button
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.NONE;
		c.ipadx = 20;
		c.ipady = 20;
		content.add(btnOK, c);
		
		c.gridx = 1;
		c.gridy = 1;
		c.fill = GridBagConstraints.NONE;
		c.ipadx = 20;
		c.ipady = 20;
		content.add(btnCancel, c);
		
		// set up event listeners
		btnOK.addActionListener(this);
		btnCancel.addActionListener(this);
		
		this.pack();
		this.setSize(getSize().width+50, getSize().height+50);
		this.setVisible(true);
	}
		
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == btnOK)
		{
			if(txtContactName.getText().length() > 0)
			{
				this.returnValue = txtContactName.getText();
				this.returnMode = RETURN_MODES.MB_OK;
				this.setVisible(false);
			} // IF
			else
				JOptionPane.showMessageDialog(null, "Please enter the contact name", "Missing Contact Name", JOptionPane.ERROR_MESSAGE);
		}
		else if(e.getSource() == btnCancel)
		{
			this.returnMode = RETURN_MODES.MB_CANCEL;
			this.setVisible(false);
		}	
	}
	
}
